<!--
 * @description  : 同级协同文书审批
 * @descriptionDetail: 同级协同文书审批
 * @copyright    : 浙江烟草
 * @author       : mwq
 * @create       : 2022-09-27 09:58:58
-->

<template>
  <view>
    <u-form
      :model="formData"
      :error-type="errorType"
      label-position="top"
      ref="uForm"
    >
      <u-card
        padding="0"
        margin="0"
        :border="false"
        :show-foot="false"
        v-if="step=='02'"
        :head-style="{ padding: '2px' }"
      >
        <template #head>
          <u-row style="padding-top: 8px; padding-bottom: 10px">
            <u-col span="12">
              <view class="card-head">
                审核审批
              </view>
            </u-col>
          </u-row>
        </template>
        <template #body>
          <!-- 审核审批 -->
          <u-form-item
            label=" 内管部门负责人处理意见："
            prop="adviseIntDpt"
            :required="true"
          >
            <u-input
              :disabled="step!=='02'"
              type="textarea"
              placeholder="请输入"
              v-model="formData.adviseIntDpt"
            />
          </u-form-item>
        </template>
      </u-card>

      <u-card
        padding="0"
        margin="0"
        :border="false"
        :show-foot="false"
        v-if="step=='03'"
        :head-style="{ padding: '2px' }"
      >
        <template #head>
          <u-row style="padding-top: 8px; padding-bottom: 10px">
            <u-col span="12">
              <view class="card-head">
                结果反馈
              </view>
            </u-col>
          </u-row>
        </template>
        <template #body>
          <u-form-item
            label=" 协同反馈（限330）"
            prop="inquireConclusion"
            :required="step=='03'"
          >
            <u-input
              :disabled="step!=='03'"
              type="textarea"
              placeholder="请输入"
              :maxlength="maxLength"
              v-model="formData.inquireConclusion"
            />
          </u-form-item>
          <u-form-item :label-width="labelWidth" label="附件上传">
            <upload-images
              :url="uploadFile"
              :length="10"
              :extForm="{fiGroupCode: $FILE_GROUP_TYPE.s_doc_word_instance, bizId: documentDoc.docInsUuid}"
              :bizId="documentDoc.docInsUuid"
              :del-url="delUrl"
              :can-add="true"
              :can-del="true"
              :renderImages="renderImages"
              ref="saveModel3"
            >
            </upload-images>
          </u-form-item>
        </template>
      </u-card>
    </u-form>
    <!-- 流程操作 -->
    <view class="footer" v-if="step!=='view' && step!=='edit'">
      <u-row gutter="16" v-if="step=='02'">
        <u-col span="6">
          <u-button type="error" @tap="handleReject" :disabled="step=='03'">
            退回
          </u-button>
        </u-col>
        <u-col span="6">
          <u-button type="primary" @tap="handleEnd">
            同意
          </u-button>
        </u-col>
      </u-row>
      <u-row gutter="16" v-if="step=='03'">
        <u-col span="12">
          <u-button type="primary" @tap="handleEnd">
            同意
          </u-button>
        </u-col>
      </u-row>
      <u-row gutter="16" v-if="step=='04'">
        <u-col span="12">
          <u-button type="primary" @tap="handleFile">
            归档
          </u-button>
        </u-col>
      </u-row>
    </view>
  </view>
</template>

<script>
import store from '@/store';
import approval from "@/service/internal/approval.service.js";
import util from "@/common/util.js";
import commonService from "@/service/internal/common.service";
import { UploadImages, } from "@/components";
export default {
  components: {  UploadImages, },
  props:{
    docIns:{
      type:Object,
      default:() => {},
    },
    step:{
      type:String,
      default:"",
    },
    stepCode:{
      type:String,
      default:"",
    },
    flowFormCode:{
      type:String,
      default:"",
    },
    approve: {
      type: Array,
      default: () => [],
    },
    documentDoc:{
      type:Object,
      default:() => ({}),
    },
  },
  data() {
    return {
      approveList:[],
      showPerson:false,
      clueShow: false,
      isdisable: true, // 全部禁用
      isresults:true, // 结果定性阶段
      labelWidth: 180,
      detailData:{},
      test1: "",
      formData: {
        // isAdopt:"",
       
        feedbackDate01:"",
        condition01:"同意", // 意见
        adviseIntDpt:"",// 审核意见
        userName02:"",// 审核人
        feedbackDate02:"",// 审核时间
      },
      dtlList:[],
      
      uploadFile: commonService.uploadFile,
      delUrl: commonService.deleteFile,
      errorType: ["message",],
      checkedPerson:[],// 复选框选中后的数据
      persons:[],
      approvePersonName:"",
      maxLength:330,
    };
  },
  watch:{
    docIns: {
      deep:true,
      immediate: true,
      handler(v) {
        console.log("tid===>>>>",v)
        if(v.listId){
          this.getFlowPerson()
          this.formData.adviseIntDpt = v.adviseIntDpt || ''
          this.formData.inquireConclusion = v.inquireConclusion || ''
        }
      },
    },
    documentDoc:{
      deep:true,
      immediate: true,
      handler(v) {
        console.log("byID", v);
      },
    },
    approve:{
      deep:true,
      handler(v) {
        this.approveList = [...v,]
        let userInfo = this.$storage.get(this.$rp.common.USER).user.personName
        let toDay = util.dateUtils.dateFormat('yyyy年MM月dd日',new Date())
        this.formData.userName01 = this.filterApproveList("01",'approvePersonName',userInfo)
        this.formData.feedbackDate01 = this.filterApproveList("01",'approveDate',toDay)
        this.formData.condition01 = this.filterApproveList("01",'approveInfo','同意')
        this.formData.adviseIntDpt = this.filterApproveList("02",'approveInfo','同意')
        this.formData.reformIntDpt = this.approveList.filter(item => item.gfaCode == '01') && this.approveList.filter(item => item.gfaCode == '01').length > 0 && this.approveList.filter(item => item.gfaCode == '01')[0].approveInfo ? this.approveList.filter(item => item.gfaCode == '01')[0].approveInfo : ""
        this.formData.condition = this.approveList.filter(item => item.gfaCode == '02') && this.approveList.filter(item => item.gfaCode == '02').length > 0 && this.approveList.filter(item => item.gfaCode == '02')[0].approveInfo ? this.approveList.filter(item => item.gfaCode == '02')[0].approveInfo : ""
        if(this.step == '01'){
          this.formData.userName02 = ''
          this.formData.feedbackDate02 = ''
          this.rules.condition01[0].required = true
        }else if(this.step == '02'){
          this.formData.userName02 = this.filterApproveList("02",'approvePersonName',userInfo)
          this.formData.feedbackDate02 = this.filterApproveList("02",'approveDate',toDay)
          this.rules.adviseIntDpt[0].required = true
        }else if(this.step == '03'){
          this.formData.userName02 = this.filterApproveList("02",'approvePersonName',userInfo)
          this.formData.feedbackDate02 = this.filterApproveList("02",'approveDate',toDay)
          this.rules.condition01[0].required = false
          this.rules.adviseIntDpt[0].required = false
        }
      },
    },
  },
  created(){
  },
  mounted(){
    console.log("状态",this.step)   
    this.$refs.uForm.setRules(this.rules);
  },
  changeHeight(){ 
    this.$refs.consitem.init();
  },
  onLoad(){
  },
  onReady() {
    this.$refs.uForm.setRules(this.rules);
  },
  methods: {
    renderImages(){
      return commonService.findFilesByBizId(this.documentDoc.docInsUuid).then(r => {
        r.data.forEach(i => {
          i.src = `${commonService.downloadFile}?fileId=${i.gfiId}&tk=${uni.getStorageSync("tk")}`;
          i.filePath = i.src;
        });
        return r.data;
      });
    },
    filterApproveList(step,stepValue,userInfo){
      return  (this.approveList.filter(item => item.gfaCode == step) && this.approveList.filter(item => item.gfaCode == step).length > 0) ? (this.approveList.filter(item => item.gfaCode == step)[0][stepValue] ? this.approveList.filter(item => item.gfaCode == step)[0][stepValue] : userInfo ? userInfo : "") : ""
    },
    getDictName(value,dicName){
      if (!value) return ''
      if (!dicName) return ''
      return store.state.dicts[dicName].filter(d => d.bcbCode === value)[0].bcbName;
    },
    handleSwitch(sw) {
      this.radioCheckWrap = sw;
    },
    // 点击同意按钮
    handleEnd(){
      if(this.step == '02' && !this.formData.adviseIntDpt){
        this.$u.toast('请输入内管部门负责人处理意见')
        return false
      }
      if(this.step == '03' && !this.formData.inquireConclusion){
        this.$u.toast('请输入协同反馈')
        return false
      }
      let toDay = util.dateUtils.dateFormat('yyyy年MM月dd日',new Date())
      if(this.step == '02'){
       
        let params = {
          gfaCode:this.step,
          nextPersons:this.docIns.inquirePersonUuids,
          approvePersonUuid:this.$storage.get(this.$rp.common.USER).user.personUuid,
          
        }
        console.log('验证通过');
        params.approveDate = toDay
        params.approveInfo = '同意'
        this.$u.confirm("将提交给" + this.docIns.inquirePersonNames).then(() => {
          this.$modal.showLoading("加载中...");
          approval.teamAgree({...this.docIns,...this.formData,},params).then(res => {
            if(res.success){
              this.showPerson = false
              this.$u.toast(res.msg)
              this.backPrev();
            }else{
              this.$u.toast('审核失败')
            }
            this.$modal.hideLoading()
          })      
        });
      }else if (this.step == '03'){ 
        let params = {
          gfaCode:this.step,
          nextPersons:'',
          approvePersonUuid:this.$storage.get(this.$rp.common.USER).user.personUuid,
        }
        params.approveDate = toDay
        params.approveInfo = '已反馈'
        this.$u.confirm("流程将提交给" + this.approvePersonName).then(() => {
          approval.teamAgree({...this.docIns,...this.formData,},params).then(res => {
            if(res.success){
              this.showPerson = false
              this.$u.toast(res.msg)
              this.backPrev();
            }else{
              this.$u.toast('反馈失败')
            }
            this.$modal.hideLoading()
          })
        })
      }
    },
    // 点击选中
    checkboxGroupChange(e){
      this.checkedPerson = e
    },
  
    handleReject(){
      if(this.step == '02' && !this.formData.adviseIntDpt){
        this.$u.toast('请输入内管部门负责人处理意见')
        return false
      }
      let userInfo = this.$storage.get(this.$rp.common.USER).user.personName
      let toDay = util.dateUtils.dateFormat('yyyy年MM月dd日',new Date())
      this.$modal.showLoading("加载中...");
      let params = {
        gfaCode:this.step,
        nextPersons:this.docIns.inquirePersonUuids,
        approvePersonUuid:this.$storage.get(this.$rp.common.USER).user.personUuid,
      }
      if(this.step == '01' || this.step == '02' || this.step == 'edit'){
        // params.approveInfo = userInfo
        if(this.step == '01'){
          params.approveInfo = this.formData.condition01 ? this.formData.condition01 : "不同意"
        }else if(this.step == '02'){
          params.approveInfo = this.formData.adviseIntDpt ? this.formData.condition02 : "不同意"
        }else{
          params.approveInfo = '不同意'
        }
        params.approveDate = toDay
        params.stepCode = ''
        approval.teamBackToFirst(this.docIns,params).then(res => {
          this.backPrev();
          this.$modal.hideLoading()
        })
      }
    },
    getFlowPerson(){
      approval.getFlowPerson(this.docIns.listId,"start").then(d => {
        let files = d.bean;
        this.approvePersonName = files.approvePersonName
      })
    },
    handleFile(){
      let toDay = util.dateUtils.dateFormat('yyyy年MM月dd日',new Date())
      { 
        let params = {
          gfaCode:this.step,
          approvePersonUuid:this.$storage.get(this.$rp.common.USER).user.personUuid,
        }
        params.approveDate = toDay
        params.approveInfo = '已归档'
        approval.teamAgree({...this.docIns,...this.formData,},params).then(res => {
          if(res.success){
            this.showPerson = false
            this.$u.toast(res.msg)
            this.backPrev();
          }else{
            this.$u.toast('归档失败')
          }
          this.$modal.hideLoading()
        }) }
    },
  },
};
</script>

<style lang="scss" scoped>
.u-form {
  // padding-left: 1%;
  // padding-right: 1%;
}
.card-head {
  color: #191f25;
  font-weight: bold;
  font-size: 28rpx;
}
  .footer{
    position: fixed;
    bottom: 5px;
    left: 0;
    width: 100%;
    z-index: 10;
  }
.popup-header-item{
    background-color: rgb(41, 121, 255);
    width: 100%;
    height: 88rpx;
    font-size: 32rpx;
    font-weight: bold;
    color: white;
    line-height: 88rpx;
    text-align: center;
  }
  .header-btn{
    float: right;
    margin-right: 5%;
    font-size: 28rpx;
  }
</style>